//
//  AYMMassageLoginController.m
//  QuickCure
//
//  Created by MootYue on 16/5/6.
//  Copyright © 2016年 ayumi. All rights reserved.
//

#import "AYMMassageLoginController.h"
#import "AYMRegisterViewController.h"
#import "AYMFindPSWController.h"
#import "SVPAlertManager.h"

@interface AYMMassageLoginController ()
//手机号
@property (weak, nonatomic) IBOutlet UITextField *phoneNumField;
//验证码
@property (weak, nonatomic) IBOutlet UITextField *verificationCodeField;
@property (weak, nonatomic) IBOutlet UIButton *sendButton;
@property (nonatomic, strong) NSTimer *timer;
@property (nonatomic, assign) int time;

@end

@implementation AYMMassageLoginController

- (void)viewDidLoad {
    [super viewDidLoad];
    //计时器时间
    self.time = 45;
    [self setUpForDismissKeyboard];
}
//获取验证码
- (IBAction)getverificationCode:(id)sender {
    if (self.phoneNumField.text.length == 11) {
        self.time = 45;
        self.sendButton.enabled = NO;
        [self.sendButton setTitleColor:[UIColor colorWithRed:24/255.0 green:188/255.0 blue:187/255.0 alpha:1] forState:UIControlStateNormal];
        [self.sendButton setBackgroundColor:[UIColor colorWithWhite:0.5 alpha:0.1]];
        [self.sendButton setTitle:@"45秒" forState:UIControlStateNormal];
        self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(AnimationOfSeconds) userInfo:nil repeats:YES];
        [SMSSDK getVerificationCodeByMethod:0 phoneNumber:self.phoneNumField.text zone:@"86" customIdentifier:self.verificationCodeField.text result:^(NSError *error) {
            if (error){
                Log(@"发送短信失败");
            }
        }];
    }else{
        [SVPAlertManager alertErrorStatus:@"手机号格式不对"];
    }
}

//点击登录
- (IBAction)LoginClick:(id)sender {
    if (self.phoneNumField.text.length == 11 && self.verificationCodeField.text.length != 0) {
        [SMSSDK commitVerificationCode:self.verificationCodeField.text phoneNumber:self.phoneNumField.text zone:@"86" result:^(NSError *error) {
//            if (error) {
//                [SVPAlertManager alertErrorStatus:@"验证码输入有误"];
//            }
        }];
        [SVPAlertManager alertErrorStatus:@"登陆错误 请通过注册账户登陆"];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self.navigationController popViewControllerAnimated:YES];
        });

    }else{
        [SVPAlertManager alertErrorStatus:@"请输入正确的手机号和验证码"];
    }
}

//回到登陆页面
- (IBAction)jumpToLogin:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

//点击注册新账号按钮
- (IBAction)registClicButtom:(id)sender {
    AYMRegisterViewController *registVC = [AYMRegisterViewController new];
    [self jumpToTargetVC:registVC];
   
}

//点击忘记密码按钮
- (IBAction)losePassWorBottm:(id)sender {

    AYMFindPSWController *findPSWVC = [[AYMFindPSWController alloc]init];
    [self jumpToTargetVC:findPSWVC];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//NSTimer的方法
-(void)AnimationOfSeconds{
    if (self.time == 0) {
        self.sendButton.enabled = YES;
        [self.sendButton setTitle:@"获取验证码" forState:UIControlStateNormal];
        [self.sendButton setTitleColor:[UIColor colorWithRed:32.0/255 green:198.0/255 blue:198.0/255 alpha:1.0] forState:UIControlStateNormal];
        [self.sendButton setBackgroundColor:[UIColor colorWithRed:236 green:236 blue:236 alpha:1]];
        self.time = 45;
        [self.timer invalidate];
        return;
    }else{
        self.time -- ;
        [UIView animateWithDuration:0.5 animations:^{
        [self.sendButton setTitle:[NSString stringWithFormat:@"%d秒",self.time] forState:UIControlStateNormal];
        }];

    }
   
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    //开启定时器
    [self.timer setFireDate:[NSDate distantPast]];
    self.time = 45;
}

//页面消失，进入后台不显示该页面，关闭定时器
-(void)viewDidDisappear:(BOOL)animated
{
    
    [super viewDidDisappear:animated];
    //关闭定时器
    [self.timer setFireDate:[NSDate distantFuture]];
    self.time = 45;
}

//取消键盘方法
- (void)setUpForDismissKeyboard {
    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
    UITapGestureRecognizer *singleTapGR =
    [[UITapGestureRecognizer alloc] initWithTarget:self
                                            action:@selector(tapAnywhereToDismissKeyboard:)];
    NSOperationQueue *mainQuene =[NSOperationQueue mainQueue];
    [nc addObserverForName:UIKeyboardWillShowNotification
                    object:nil
                     queue:mainQuene
                usingBlock:^(NSNotification *note){
                    [self.view addGestureRecognizer:singleTapGR];
                }];
    [nc addObserverForName:UIKeyboardWillHideNotification
                    object:nil
                     queue:mainQuene
                usingBlock:^(NSNotification *note){
                    [self.view removeGestureRecognizer:singleTapGR];
                }];
}

- (void)tapAnywhereToDismissKeyboard:(UIGestureRecognizer *)gestureRecognizer {
    [self.view endEditing:YES];
}

@end
